using System;
using Slaks.DocumentSerialization.Document;
using Slaks.DocumentSerialization.TermIdInfo;

namespace Slaks.Graphs.GraphBuilder.Slaks
{
	/// <summary>
	/// Summary description for NDistanceRepresentationSlaks.
	/// </summary>
	public class NDistanceBiderectionalRepresentationSlaks : SlaksBuilder
	{
		#region IRepresentation Members

	
	    protected override Graph Build(Document document, int numberOfVerticesToLeave, int nParameter,TermIdInfoMap map)
		{
			Graph graph = base.Build(document,numberOfVerticesToLeave,nParameter,map);
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			graph.Name = documentName;
			Graph tempGraph = this.BuildTemporalGraph(document, BuilderType.NDistanceBiderectionalRepresentationSlaks);
			this.RemoveInfrequentVerticesInTemporalGraph(tempGraph, numberOfVerticesToLeave);

			Vertex vertex;
			string sectionType;

			int nDistanceCount;
			//iterate through nodes
			foreach (Node node in document.Nodes)
			{
				//check if the vertex is in the list of frequent nodes.
				if (tempGraph.GetVertex(node.Id) == null) continue;
				vertex = graph.CreateAddVertex(node.Id);
				vertex.Frequency = node.TotalTermFrequency;

				//iterate through sections
				foreach (Section section in node.Sections)
				{
					sectionType = section.GetSectionTypeString();

					Section.SectionType sectionTp = Section.GetSectionTypeByString(sectionType);

					nDistanceCount = 1;
					//iterate through sentences
					foreach (Sentence sentence in section.Sentences)
					{
						AddRelationsBiderectional(document, graph, tempGraph, vertex, nParameter, nDistanceCount, sentence, sectionTp, BuilderType.NDistanceRepresentationSlaks);
					}
				}
			}
			return graph;
		}

		#endregion
	}
}